<template>
  <x-main>
    <x-table ref="table" :optionWidth="180" :isPage="true" @search="onSearch" @view="onView" @modify="onModify" @remove="onRemove">
      <template v-slot:btn-tool>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </template>
      <template #status="scope">
        <el-tag v-if="scope.data.status == 0">正常</el-tag>
        <el-tag type="danger" v-if="scope.data.status == 9">冻结</el-tag>
      </template>
    </x-table>
  </x-main>
</template>

<script setup name="tenant-index">
import useStore from '@/stores/modules/platform/tenant';
const store = useStore();
import XForm from './Form.vue';
import {defineProps, onMounted, ref} from "vue";

const props = defineProps({
  hospital: {
    type: Object,
    default: () => {return {}}
  }
})

let table = ref();
onMounted(() => {
  store.createTable(table);
  store.setForm(XForm);
  store.load({hospitalId: props.hospital.id});
})


/**
 * 搜索
 * @param pageNo
 * @param pageSize
 * @param condition
 */
const onSearch = (condition) => {
  store.search(0, 0, condition);
}



/**
 * 新增
 * @returns {Promise<void>}
 */
const onAdd = async () => {
  open(XForm, "新增渠道", {}, 900)
}


/**
 * 修改
 * @param row
 * @returns {Promise<void>}
 */
const onModify = async (row) => {
  let info = await store.getInfo(row.id);
  if (! info) {
    return
  }

  open(XForm, "修改渠道", info, 900)
}

/**
 * 删除
 * @param row
 */
const onRemove = (row) => {
  store.remove([row.id])
}

const onView = (row) => {
  Msg.warn("功能开发中");
}
const open = (component, title, data, width, disabled) => {
  const op = Dialog.open(component, title)
  op.setWidth(width)

  op.mounted(c => {
    c.render(data, disabled)
  })

  op.confirm(c => {
    c.submit()
  })
}

</script>
